package com.sinoeyes.controller.compent;

import com.sinoeyes.common.constant.Constants;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.result.R;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.utils.bean.ObjectUtils;
import com.sinoeyes.compent.po.Customer;
import com.sinoeyes.compent.service.ICustomerService;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 客户集（代理商，医药公司，医院）相关模块
 *
 * @author aries.hao
 */
@Api(value = "Customer API", tags = {"客户集信息接口"})
@RestController
@Slf4j
@RequestMapping("/customer")
public class ICustomerController extends BaseController {

    private final ICustomerService customerService;

    @Autowired
    public ICustomerController(ICustomerService customerService) {
        this.customerService = customerService;
    }

    @ApiOperation("获取单个客户信息")
    @ApiImplicitParam(name = "customerId", value = "客户ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getCustomerById/{customerId}")
    public String getDimensionInfoById(@PathVariable String customerId) {
        log.info("接收到的查询客户ID：" + customerId);
        Customer customer = customerService.selectCustomerById(customerId);
        if (ObjectUtils.isNotEmpty(customer)) {
            return convertJsonWithDate(R.success(customer));
        }
        return jsonResult(R.failure("未获取到此客户相关信息"));
    }

    @ApiOperation("条件获取客户信息")
    @PostMapping("/getCustomerList")
    public String getProductByFilter(@RequestBody(required = false) Customer customer) {
//        startPage();
//        List<Customer> customerList = customerService.selectCustomerList(customer);
//        if (!CollectionUtils.isEmpty(customerList)) {
//            return convertJsonWithDate(R.success(getRestTable(customerList)));
//        }
        return jsonResult(R.failure("未获取到客户信息！"));
    }

    @ApiOperation("添加客户信息")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addCustomer")
    public String addCustomer(@RequestBody Customer customer) {
        log.info("###########客户信息：" + customer.toString());
        int i = customerService.insertCustomer(customer);
        if (i > 0) {
            return jsonResult(R.success("客户信息添加成功"));
        }
        return jsonResult(R.failure("客户信息添加失败"));
    }

    @ApiOperation("更新客户信息")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @ApiImplicitParam(name = "customer", value = "修改客户信息", dataType = "Customer")
    @PostMapping("/updateCustomer")
    public String updateProduct(@RequestBody Customer customer) {
        log.info("###########客户信息：" + customer.toString());
        int i = customerService.updateCustomer(customer);
        if (i > 0) {
            return jsonResult(R.success("客户信息修改成功"));
        }
        return jsonResult(R.failure("客户信息修改失败"));
    }

    @ApiOperation("删除客户信息")
    @Log(title = "客户管理", businessType = BusinessType.OTHER)
    @ApiImplicitParam(name = "customerId", value = "客户ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/deleteCustomer/{customerId}")
    public String deleteDimension(@PathVariable String customerId) {
        RCode rCode = customerService.deleteCustomerByIds(customerId, Constants.DEL_FLAG);
        if (rCode.getOperateCount() > 0) {
            return jsonResult(R.success("客户信息删除成功"));
        }
        return jsonResult(R.failure("客户信息删除失败[" + rCode.getMessage() + "]"));
    }

    @ApiOperation("启用客户信息")
    @ApiImplicitParam(name = "customerId", value = "客户ID", required = true, dataType = "String", paramType = "path")
    @PutMapping("/enableCustomer/{customerId}")
    public String enableDimension(@PathVariable String customerId) {
        RCode rCode = customerService.deleteCustomerByIds(customerId, Constants.ENABlE_FLAG);
        if (rCode.getOperateCount() > 0) {
            return jsonResult(R.success("客户信息启用成功"));
        }
        return jsonResult(R.failure("客户信息启用失败[" + rCode.getMessage() + "]"));
    }
}
